package edu.bbu.license.client.android.gui.guest;

import java.util.LinkedList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.controller.guest.GuestController;
import edu.bbu.license.client.android.gui.BaseActivity;
import edu.bbu.license.client.android.gui.files.FilesActivity;
import edu.bbu.license.client.android.gui.guest.adapter.GuestActivityPagerAdapter;

public class GuestActivity extends BaseActivity implements GuestView {
		
	private ViewPager viewPager;
	private LinkedList<View> viewPagerList;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guest_activity);
				
		viewPagerList = new LinkedList<View>();
		viewPagerList.add(getLayoutInflater().inflate(R.layout.guest_activity_login_page, null));
		viewPagerList.add(getLayoutInflater().inflate(R.layout.guest_activity_register_page, null));
		viewPagerList.add(getLayoutInflater().inflate(R.layout.guest_activity_forgotpwd_page, null));
		
		viewPager = (ViewPager) findViewById(R.id.guest_activity_pager);
		viewPager.setAdapter(new GuestActivityPagerAdapter(viewPagerList));
		
		GuestController.getInstance().atach(this);
	}
	
	@Override
	public void setActivePage(final int page) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				if(page >= LOGIN_PAGE && page <= FORGORPWD_PAGE) {
					viewPager.setCurrentItem(page, true);
				}
			}
		});
	}
	
	@Override
	public void openFilesView() {
		startActivity(new Intent(this, FilesActivity.class));		
	}
	
	@Override
	public void closeGuestView() {
		finish();
	}
	
	@Override
	public void setHeaderLoginButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.guest_activity_header_login_button).setOnClickListener(onClickListener);
	}
	
	@Override
	public void setHeaderRegisterButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.guest_activity_header_register_button).setOnClickListener(onClickListener);
	}
	
	@Override
	public void setHeaderForgotPwdButtonListener(OnClickListener onClickListener) {
		findViewById(R.id.guest_activity_header_forgotpwd_button).setOnClickListener(onClickListener);
	}
	
	@Override
	public void setGuestLoginButtonListener(OnClickListener onClickListener) {
		viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_button).setOnClickListener(onClickListener);	
	}
	
	@Override
	public void setGuestRegisterButtonListener(OnClickListener onClickListener) {
		viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public void setGuestForgotPwdButtonListener(OnClickListener onClickListener) {
		viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_button).setOnClickListener(onClickListener);		
	}
	
	@Override
	public String getLoginEmail() {
		EditText emailEditText = (EditText) viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_email);
		return emailEditText.getText().toString();	
	}
	
	@Override
	public String getLoginPassword() {
		EditText passwordEditText = (EditText) viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_password);
		return passwordEditText.getText().toString();	
	}
	
	@Override
	public void setLoginError(int resource) {
		setLoginError(getString(resource));
	}
	
	@Override
	public void setLoginError(final String message) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_error_label);
				textView.setText(message);
			}
		});
	}
	
	@Override
	public void resetLoginError() {
		setLoginError("");
	}
	
	@Override
	public void showLoginProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_button).setVisibility(View.GONE);
				viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_progress_bar).setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public void hideLoginProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_progress_bar).setVisibility(View.GONE);
				viewPagerList.get(LOGIN_PAGE).findViewById(R.id.guest_activity_login_button).setVisibility(View.VISIBLE);
			}
		});			
	}
	
	@Override
	public String getRegisterEmail() {
		EditText emailEditText = (EditText) viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_email);
		return emailEditText.getText().toString();	
	}
	
	@Override
	public String getRegisterConfEmail() {
		EditText emailEditText = (EditText) viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_confemail);
		return emailEditText.getText().toString();	
	}
	
	@Override
	public void setRegisterError(int resource) {
		setRegisterError(getString(resource));
	}
	
	@Override
	public void setRegisterError(final String message) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_error_label);
				textView.setText(message);	
			}
		});			
	}
	
	@Override
	public void resetRegisterError() {
		setRegisterError("");	
	}
	
	@Override
	public void showRegisterProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_button).setVisibility(View.GONE);
				viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_progress_bar).setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public void hideRegisterProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_progress_bar).setVisibility(View.GONE);
				viewPagerList.get(REGISTER_PAGE).findViewById(R.id.guest_activity_register_button).setVisibility(View.VISIBLE);
			}
		});			
	}
	
	@Override
	public String getForgotPwdEmail() {
		EditText emailEditText = (EditText) viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_email);
		return emailEditText.getText().toString();
	}
	
	@Override
	public void setForgotPwdError(int resource) {
		setForgotPwdError(getString(resource));
		
	}
	
	@Override
	public void setForgotPwdError(final String message) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView textView = (TextView) viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_error_label);
				textView.setText(message);					
			}
		});
	}
	
	@Override
	public void resetForgotPwdError() {
		setForgotPwdError("");
	}
	
	@Override
	public void showForgotPwdProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_button).setVisibility(View.GONE);
				viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_progress_bar).setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public void hideForgotPwdProgressIndicator() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_progress_bar).setVisibility(View.GONE);
				viewPagerList.get(FORGORPWD_PAGE).findViewById(R.id.guest_activity_forgotpwd_button).setVisibility(View.VISIBLE);
			}
		});
	}
}
